<template>
  <el-dialog
    :append-to-body="true"
    :close-on-click-modal="false"
    :before-close="cancel"
    :visible.sync="dialog"
    :title="isAdd ? '新增' : '编辑'"
    width="500px"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="上课教室" prop="collegeId">
        <el-input v-model="form.classroom" style="width: 350px;"/>
      </el-form-item>
      <el-form-item label="选择课程" prop="specialtyId">
        <el-select v-model="form.cid" placeholder="请选择">
          <el-option
            v-for="item in course"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="选择课时" prop="specialtyId">
        <el-select v-model="form.sectionId" placeholder="请选择">
          <el-option
            v-for="item in section"
            :key="item.id"
            :label="item.section +' '+ item.week"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="选择周数" prop="specialtyId">
        <el-select v-model="form.weekId" placeholder="请选择">
          <el-option
            v-for="item in week"
            :key="item.id"
            :label="item.time"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程总人数" prop="specialtyId">
        <el-input v-model="form.totalPeople" style="width: 350px;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import common from '@/api/common/common'
  import course from '@/api/teacher/course/course'

  export default {
    props: {
      isAdd: {
        type: Boolean,
        required: true
      }
    },
    data() {
      return {
        loading: false, dialog: false,
        form: {
          id: null,
          sectionId: null,
          cid: null,
          totalPeople: null,
          classroom: null,
        },
        params: {
          systemId: 4
        },
        section: [],
        week: [],
        course: [],
      }
    },
    created() {
      common.listajaxWeek().then(res => {
        this.week = res.records;
      })
      common.listajaxSection().then(res => {
        this.section = res.records;
      })
      common.listajaxCourse(this.params).then(res => {
        this.course = res;
      })
    },
    methods: {
      cancel() {
        this.resetForm()
      },
      doSubmit() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.isAdd) {
              this.doAdd()
            } else {
              this.doEdit()
            }
          }
        })
      },
      doAdd() {
        course.teacherCourseAdd(this.form).then(res => {
          this.resetForm()
          this.loading = false
          this.$emit('close')
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      doEdit() {
        course.teacherCourseEdit(this.form).then(res => {
          this.resetForm()
          this.loading = false
          this.$emit('close')
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      resetForm() {
        this.dialog = false
        this.$refs['form'].resetFields()
        this.form = {
          id: null,
          sectionId: null,
          cid: null,
          totalPeople: null,
          classroom: null,
        }
      },
    }
  }
</script>

<style scoped>

</style>
